package com.leyou.service;

import com.leyou.common.utils.NumberUtils;
import com.leyou.mapper.UserMapper;
import com.leyou.user.pojo.User;
import com.leyou.utils.CodecUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by XING CHAO on 2020/1/3 10:28
 */

@Service
public class UserService {

   @Autowired
    private UserMapper userMapper;
   @Autowired
   private AmqpTemplate amqpTemplate;

   @Autowired
   private StringRedisTemplate redisTemplate;

    static final String KEY_PREFIX = "user:verify";

    public Boolean checkUserData(String data, Integer type) {
        User user = new User();
        switch (type){
            case 1:
                user.setUsername(data);
                break;
            case 2:
                user.setPhone(data);
                break;
            default:
                return  null;
        }
        return this.userMapper.selectCount(user) ==0;

    }

    //发送手机验证码
    public void sendVerifyCode(String phone) {
        //验证手机号是否为空
        if(StringUtils.isBlank(phone)){
            return ;
        }
        //校验

        //生成验证码，
        String code = NumberUtils.generateCode(6);

        //消息内容是map类型
        Map<String, String> msgMap = new HashMap<>();
        msgMap.put("phone", phone);
        msgMap.put("code", code);
        //发送消息给队列
        this.amqpTemplate.convertAndSend("LEYOU.SMS.EXCHANGE","user.verify",msgMap);

        //缓存验证码
        this.redisTemplate.opsForValue().set(KEY_PREFIX + phone, code);
    }

    /**
     *用户注册
     * @Date  2020/1/3 20:20
     * @param user
     * @param code
     * @return
     **/
    public void register(User user, String code) {
        //校验短信验证码
        String cacheCode =this.redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        if(!StringUtils.equals(cacheCode,code)){
            //验证码不合法
            return ;
        }

        //生成盐
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);

        //对密码加密
        user.setPassword(CodecUtils.md5Hex(user.getPassword(),salt));
        //强制设置不能指定的参数为null
        user.setId(null);
        user.setCreated(new Date());

        // 添加到数据库
        this.userMapper.insertSelective(user);
        //添加成功，删除redis中的记录
        this.redisTemplate.delete(KEY_PREFIX + user.getPhone());
    }
    /***
     *根据用户名和密码查询用户
     * @Date  2020/1/3 21:20
     * @param username
     * @param password
     * @return
     **/
    public User queryUser(String username, String password) {
        User record = new User();
        record.setUsername(username);
        User user = this.userMapper.selectOne(record);

        //进行校验
        if(user ==null){
            return  null;
        }

        //校验密码，  对传递的密码进行加密，判断加密之后的密码是否一致
        password=CodecUtils.md5Hex(password, user.getSalt());
        if (!StringUtils.equals(password,user.getPassword())) {
            return null;
        }
        // 用户名密码都正确
        return user;
    }
}
